﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Corners.PassByRefAndVal;

namespace Corners
{
    public class CornerExplorer
    {
        public static void Main(string[] args)
        {
            DemoPassByRef();
        }
        

        private void DemoPassByRefAndVal()
        {
            //http://msdn.microsoft.com/en-us/library/8b0bdca4.aspx

            /*
             
             Because a struct is a value type, when you pass a struct by value to a method, the method receives and operates on a copy of the struct argument. 
             The method has no access to the original struct in the calling method and therefore can't change it in any way. The method can change only the copy.
             
             A class instance is a reference type, not a value type. When a reference type is passed by value to a method, the method receives a copy of the 
             reference to the class instance. That is, the method receives a copy of the address of the instance, not a copy of the instance itself. 
             The class instance in the calling method has an address, the parameter in the called method has a copy of the address, and both addresses refer to the
             same object. 
             
             Because the parameter contains only a copy of the address, the called method cannot change the address of the class instance in the calling method. 
             However, the called method can use the address to access the class members that both the original address and the copy reference. 
             
             If the called method changes a class member, the original class instance in the calling method also changes.
             
             */

            TheClass testClass = new TheClass();
            TheStruct testStruct = new TheStruct();

            testClass.willIChange = "Not Changed";
            testStruct.willIChange = "Not Changed";

            TestClassAndStruct.ClassTaker(testClass);
            TestClassAndStruct.StructTaker(testStruct);

            Console.WriteLine("Class field = {0}", testClass.willIChange);
            Console.WriteLine("Struct field = {0}", testStruct.willIChange);

            // Keep the console window open in debug mode.
            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }


        private static void DemoPassByRef()
        {
            int[] arr = { 1, 4, 5 };
            System.Console.WriteLine("Inside Main, before calling the method, the first element is: {0}", arr[0]);

            Change(arr);
            System.Console.WriteLine("Inside Main, after calling the method, the first element is: {0}", arr[0]);
        }


        static void Change(int[] pArray)
        {
            pArray[0] = 888;  // This change affects the original element.
            pArray = new int[5] { -3, -1, -2, -3, -4 };   // This change is local.
            System.Console.WriteLine("Inside the method, the first element is: {0}", pArray[0]);
        }

    }
}
